//
//  GroupActiveTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/2/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "GroupActiveTableViewCell.h"

@interface GroupActiveTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UIImageView * iconImageView;
@property (nonatomic, strong)UIImageView * timeImageView;
@property (nonatomic, strong)UIImageView * localImageView;
@property (nonatomic, strong)UIImageView * personImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UILabel * personLabel;
@property (nonatomic, copy)NSString * groupID;

@end

@implementation GroupActiveTableViewCell


- (void)setModel:(GroupActiveModel *)model{
    
    _model = model;
    
    self.titleLabel.text = model.activity_title;
    self.timeLabel.text = model.activity_time;
    self.localLabel.text = model.merchants_address;
    self.personLabel.text = [NSString stringWithFormat:@"%@",model.participation_count];
    
    NSArray * imgArr = [model.merchants_goods_img componentsSeparatedByString:@","];    
    NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,imgArr.firstObject];
    NSURL * url = [NSURL URLWithString:imgStr];
    [self.iconImageView sd_setImageWithURL:url];
    
    self.groupLabel.text = [NSString stringWithFormat:@"<%@>",model.group_name];
    self.groupID = [NSString stringWithFormat:@"%@",model.group_id];
    
    int is_join = [model.is_join intValue];
    
    if (is_join == 1) {
    
        self.joinImageView.hidden = NO;
    }else{
        
        self.joinImageView.hidden = YES;
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor  =ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    self.iconImageView = [self setupImageViewWithImg:@""];
    self.iconImageView.backgroundColor = [UIColor grayColor];
    [self.backView addSubview:self.iconImageView];
    self.timeImageView = [self setupImageViewWithImg:@"话题时间"];
    [self.backView addSubview:self.timeImageView];
    self.localImageView = [self setupImageViewWithImg:@"活动地址"];
    [self.backView addSubview:self.localImageView];
    self.personImageView = [self setupImageViewWithImg:@"人数"];
    [self.backView addSubview:self.personImageView];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor =ZRBlackColor;
    titleLabel.text = @"世界那么多大，我想去看看";
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.backView addSubview:self.titleLabel];
    
    
    self.timeLabel = [self setupLabelWithTitle:@"周六 08:30"];
    [self.backView addSubview:self.timeLabel];
    self.localLabel = [self setupLabelWithTitle:@"轻旅社（天府三街吉泰六路184号"];
    [self.backView addSubview:self.localLabel];
    self.personLabel = [self setupLabelWithTitle:@"45"];
    [self.backView addSubview:self.personLabel];
    
    UILabel * groupLabel = [UILabel new];
    
    groupLabel.textColor = ColorWithRGB(0x4C87D8);
    groupLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    groupLabel.textAlignment = NSTextAlignmentRight;
    groupLabel.hidden = YES;
    groupLabel.userInteractionEnabled = YES;
    //创建轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [groupLabel addGestureRecognizer:tapGR];
    
    self.groupLabel = groupLabel;
    
    [self.backView addSubview:self.groupLabel];
    
    
    UIImageView * joinImageView = [UIImageView new];
    
    joinImageView.image = [UIImage imageNamed:@"已参加"];
    joinImageView.hidden = YES;
    
    self.joinImageView = joinImageView;
    
    [self.backView addSubview:self.joinImageView];
}


-(void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(100 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.mas_equalTo(self.backView);
        make.width.mas_equalTo(110 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(5 * SCREENH6);
    }];
    
    [self.timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(13 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeImageView.mas_trailing).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.timeImageView);
    }];
    
    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeImageView);
        make.top.mas_equalTo(self.timeImageView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(12 * SCREENH6);
        make.height.mas_equalTo(14 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.timeLabel);
        make.centerY.mas_equalTo(self.localImageView);
    }];
    
    [self.personImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.localImageView);
        make.top.mas_equalTo(self.localImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.personLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.localLabel);
        make.centerY.mas_equalTo(self.personImageView);
    }];
    
    [self.groupLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.personLabel);
    }];
    
    [self.joinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.personImageView);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
        make.width.mas_equalTo(45 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
}


- (UIImageView *)setupImageViewWithImg:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    return label;
}


- (void)tapAction{
    
    NSDictionary * dict = @{@"group_id":self.groupID};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyActiveToGroupInfo" object:nil userInfo:dict];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
